﻿using Juli.Mes.Extensions;
using Juli.Mes.MongoDb.Repositories;
using Juli.Mes.Mqtt.Etos;
using MongoDB.Bson;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.EventBus;
using Volo.Abp.Identity;

namespace Juli.Mes.EventHandlers
{
    public class PublishTelemetryEventHandler : ILocalEventHandler<ClientPublishTelemetryEto>, ITransientDependency
    {
        private readonly ITelemetryRepository _telemetryRepository;

        public PublishTelemetryEventHandler(ITelemetryRepository telemetryRepository)
        {
            _telemetryRepository = telemetryRepository;
        }

        public Task HandleEventAsync(ClientPublishTelemetryEto eventData)
        {
            //存到mongo

            //var document = new BsonDocument
            //{
            //    { "name", "John Doe" },
            //    { "age", 25 },
            //    { "email", "johndoe@example.com" }
            //};

            var dic = eventData.Payload.ConvertPayloadToDictionary();

            var document = dic.ToBsonDocument();

            _telemetryRepository.AddBsonAsync(document);

            return Task.FromResult(true);

        }
    }
}
